<?php
namespace app\shop\controller;
use think\Controller;
use think\request;
use think\Db;
use app\shop\controller\Common;
use app\service\shopService;
use app\service\lifeapiService;
class User extends Common{
	//用户中心
	public function center(){
		
		session('url',request()->url());
		$this->assign("bottom_type","4");
		$this->assign("title","个人中心");

		$userinfo=(new shopService())->userFind(session('user_id'));
		
		$this->assign('userinfo',$userinfo);
		return $this->fetch();
	}
	//未支付订单
	public function unpaidOrder(){
		session('url',request()->url());
		//session("user_id","2088112618452297");
		$this->assign("bottom_type","4");
		$this->assign("title","未支付订单");
		
		$orderlist=Db::table('zz_order_online')->where('order_status','0')->where('uid',session('user_id'))->order('order_id desc')->select();
		//根据订单列表遍历订单详情
		foreach ($orderlist as $key => $value) {
			$orderlist[$key]["order_goods"]=DB::table('zz_order_goods')->where("order_id",$value["order_id"])->select();
		}
		$this->assign("orderlist",$orderlist);
		return $this->fetch('unpaid_order');
	}
	//线上已支付订单列表
	public function paidOrdertest(){
		session('url',request()->url());
		
		$this->assign("bottom_type","4");
		$this->assign("title","已订单列表");
		
		$orderlist=Db::table('zz_order_online')->where('order_status','1')->where('uid',session('user_id'))->order('order_id desc')->select();
		//根据订单列表遍历订单详情
		foreach ($orderlist as $key => $value) {
			$orderlist[$key]["order_goods"]=DB::table('zz_order_goods')->where("order_id",$value["order_id"])->select();
		}
		$this->assign("orderlist",$orderlist);
		return $this->fetch('paid_order_test');
	}
	//线上已支付订单列表
	public function paidOrder(){
		session('url',request()->url());
		
		$this->assign("bottom_type","4");
		$this->assign("title","已订单列表");
		
		$orderlist=Db::table('zz_order_online')->where('order_status','1')->where('uid',session('user_id'))->order('order_id desc')->select();
		
		$this->assign("orderlist",$orderlist);
		return $this->fetch('paid_order');
	}
	//商城订单详情
	public function orderinfo(){
		session("url",request()->url());
		
		$order_id=request()->get("order_id");
		$orderinfo=Db::table("zz_order_online")->where("order_id",$order_id)->find();
		$ordergoods=Db::table("zz_order_goods")->where("order_id",$order_id)->select();
		$this->assign("orderinfo",$orderinfo);
		$this->assign("ordergoods",$ordergoods);
		$this->assign("title","订单详情");
		
		return $this->fetch('order_info');
	}
	//收货地址列表
	public function address(){
		session('url',request()->url());
		$this->assign("bottom_type","4");
		$this->assign("title","收货地址管理");
		$data=Db::table('zz_address')->where('uid',session('user_id'))->select();
		$this->assign("data",$data);
		return $this->fetch('');
	}
	//选择收货地址
	public function choiceAddress(){
		session('url',request()->url());
		$shop_id=request()->get("shop_id");
		$this->assign("bottom_type","4");
		$this->assign("title","选择收货地址");
		$this->assign("shop_id",$shop_id);
		$data=Db::table('zz_address')->where('uid',session('user_id'))->select();
		$this->assign("data",$data);
		return $this->fetch('choice_address');
	}
	//增加收货地址
	public function addAddress(){
		session('url',request()->url());
		if(request()->isPost()){
			$address["room_id"]=request()->post("roomid");
			
			$address["receive_name"]=request()->post("name");
			$address["phone"]=request()->post("phone");
			//echo "<pre>";
			//var_dump(request()->post());
			$roominfo=Db::table("roominfo")->where("id",request()->post("roomid"))->find();
			$community=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
			$address["out_room_id"]=$roominfo["out_room_id"];
			$address["community_id"]=$roominfo["community_id"];
			$address["room_address"]=$roominfo["address"];
			$address["community_name"]=$community["community_name"];
			$address["uid"]=session("user_id");
			$res=Db::table("zz_address")->insert($address);
			if($res){
				
				$url=url('User/address');
				echo "<script>alert('添加成功');location.href='$url'</script>";
				exit;
				$this->success("添加成功","address");
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->assign("bottom_type","4");
			$this->assign("title","新增收货地址");

			$community=Db::table("community")->order('sort')->select();
	        $this->assign("community",$community);
			return $this->fetch('add_address');
		}	
	}

	//默认收获地址设置
	public function addressDefault(){
		$address_id=request()->get("address_id");
		
		$res=Db::table("zz_address")->where("address_id",$address_id)->update(["default"=>'1']);
		if($res){
			Db::table("zz_address")->where("uid",session('openid'))->where('address_id','neq',$address_id)->update(["default"=>'0']);
		}
		echo $res;
	}
	//收获地址删除
	public function addressDel(){
		$address_id=request()->get("address_id");
		
		$res=Db::table("zz_address")->where("address_id",$address_id)->delete();;
		echo $res;
	}
	//积分规则
	public function pointrule(){
		return $this->fetch("point_rule");
	}
	//积分攻略
	public function pointstrategy(){
		return $this->fetch("point_strategy");
	}

	//线下订单列表
	public function offlineOrder(){
		session("url",request()->url());
		$where["buyer_id"]=session('user_id');
		$orderlist=(new shopService())->shoporderlist($where);
		$this->assign("orderlist",$orderlist);
		return $this->fetch('offline_order');
	}
}